﻿using DDD.Simple.DomainBase;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Dealer.Domain.AggregateModels.DealerAggregate
{
    public class Dealer
        : IAggregationRoot
    {
        private readonly IDealerRepository _dealerRepository;
        public Dealer(IDealerRepository dealerRepository)
        {
            _dealerRepository = dealerRepository;
            Contacts = new HashSet<Contact>();
        }
        public string Code { get; set; }
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Mobile { get; set; }
        public decimal TotalEleAmount { get; set; }
        public decimal Total { get; set; }
        public decimal TotalPVs { get; set; }
        public CardType CardType { get; set; }
        public Level Level { get; set; }
        public int SubCount { get; set; }
        public virtual ICollection<Contact> Contacts { get; set; }
        public virtual DealerTree DealerTree { get; set; }

        public async Task<Dealer> RegisteDealerAsync(string name,
            string mobile,
            Guid? parentDealerId,
            decimal eleAmount)
        {
            Id = Guid.NewGuid();
            Code = $"Code{name}";
            Name = name;
            Mobile = mobile;
            TotalEleAmount = eleAmount;
            if (eleAmount<2000)
            {
                CardType = CardType.OrdinaryMember;
            }
            else if (eleAmount>=2000&&eleAmount<4000)
            {
                CardType = CardType.SilverMember;
            }
            else
            {
                CardType = CardType.GoldMember;
            }
            Level = Level.DistrictManager;
            Total = 0;
            TotalPVs = 0;
            SubCount = 0;
            DealerTree =await new DealerTree(_dealerRepository).CreateDealerTreeAsync(Id,parentDealerId);
            return this;
        }
    }
}
